# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=jemalloc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.2.1
pkgrel=1
pkgdesc="General-purpose scalable concurrent malloc implementation (mingw64)"
arch=('any')
url="http://jemalloc.net/"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-docbook-xsl")
options=(strip staticlibs !debug)
source=("https://github.com/jemalloc/jemalloc/releases/download/${pkgver}/${_realname}-${pkgver}.tar.bz2"
        001-fix-library-extension.patch
        002-makefile.patch)
sha256sums=('34330e5ce276099e2e8950d9335db5a875689a4c6a56751ef3b1d8c537f887f6'
            '74569f3e709855f53b83212efa7641b1b4b4f84442ba8c0729813bc214f18230'
            '2d92c7e273a25e4a2f12927a3418c4ff8744962eb941f838e35f94bee46ec841')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-fix-library-extension.patch
  patch -p1 -i ${srcdir}/002-makefile.patch
}

build() {
  declare -a _btype
  if check_option "debug" "y"; then
    _btype="--enable-debug --enable-log"
  else
    _btype=
  fi

  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  CFLAGS+=" -std=gnu11"
  _XMLROOT=$(ls -d ${MINGW_PREFIX}/share/xml/docbook/xsl-stylesheets* | tail -n1)
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-autogen \
    --with-xslroot=${_XMLROOT} \
    ${_btype}

  make
}

check() {
  cd "${srcdir}"/build-${CARCH}
  make check || true
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make DESTDIR=${pkgdir} install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
